<?php


namespace Home\Model;
use Think\Model;

class  LeavesModel extends CommonModel {
	// 自动验证设置
	//protected $_validate = array( array('name', 'require', '标题必须', 1), array('content', 'require', '内容必须'), );
	
	//添加请假
    public function  do_save($data){
    
        
        $flow = new FlowModel();
        $data['confirm'] = $flow->_conv_auditor($data['confirm']);
        $this->create($data);
        $leaveId =  $this->add();
        if($leaveId){
            //添加审核
           $confirm = $flow->_conv_auditor($data['confirm']);
           $this->next_step($leaveId, $data['step']);
        }
        
        return $leaveId;
    }

    
    public function do_update($data){
        $flow = new FlowModel();
        $data['confirm'] = $flow->_conv_auditor($data['confirm']);
        $respone =['code'=>0,'msg'=>'ok'];
        $logsModel = D('LeaveLog');
        //查询是否已经有审批过的记录
        $logs = $logsModel->where("flow_id={$data['id']} and result is not null and is_del=0")->find();
        if($logs){
            $respone['code']=-1;
            $respone['msg']='已有审批操作,无法编辑';
            return $respone;
            //如果有记录,则不能修改请假信息
        }
               
        $res=$this->where("id={$data['id']}")->save($data);
        if($res){
           
            $logsModel->where("flow_id={$data['id']}")->setField('is_del','1');
        }
        $this->next_step($data['id'], 20);
        return $respone;
    }


    public function next_step($flow_id, $step) {
      
        $confirm = M("Leaves") -> where(array('id' => $flow_id)) -> getField("confirm");    
        if (!empty($confirm)&&($step==20)){
            $confirm_list = array_filter(explode("|", $confirm));
            $is_include_presenter = array_search(get_emp_no(), $confirm_list);
            if ($is_include_presenter !== false) {
                $step = $step + $is_include_presenter + 1;
            }
        }
       
        $model = D("Leaves");
        if (substr($step, 0, 1) == 2) {
           
            if ($this -> is_last_confirm($flow_id)) {
                $model -> where(array('id' => $flow_id)) -> setField('step', 40);
               
                $step = 40;
            } else {
                $step++;
            }
        }
       
        
    
     
    
        if ($step == 40) {
            $model -> where(array('id' => $flow_id)) -> setField('step', 40);
    
            $flow = M("Leaves") -> find($flow_id);
            $push_data['type'] = '审批';
            $push_data['action'] = '审核通过';
            $push_data['title'] = $flow['name'];
            $push_data['content'] = '审核人：' . get_dept_name() . "-" . get_user_name();
            $push_data['url'] = U('Flow/read',"id={$flow_id}&return_url=Flow/index");
    
            send_push($push_data, $flow['user_id']);
    
        } else {
            $data['flow_id'] = $flow_id;
            $data['step'] = $step;
            $data['emp_no'] = $this -> duty_emp_no($flow_id, $step);
           
           
            if (strpos($data['emp_no'], ",") !== false) {
                $emp_list = explode(",", $data['emp_no']);
                foreach ($emp_list as $emp) {
                    $data['emp_no'] = $emp;
                    $data= $this->log_before_insert($data, null);
                    $model = D("LeaveLog");
                    $model -> create($data);
                    $model -> add();
                }
            } else {
                $model = D("LeaveLog");
                $data= $this->log_before_insert($data, null);
                $model -> create($data);
                $model -> add();
            }
        }
    }

    
    function log_before_insert(&$data,$options){
        $emp_no = $data["emp_no"];
        $where['emp_no']=array('eq',$emp_no);
        $user_id=M("User")->where($where)->getField("id");
        $user_name=M("User")->where($where)->getField("name");
        $data["uid"]=$user_id;
        $data["user_name"]=$user_name;
        $data["type"]='leave';
        $data["create_time"]=time();
        $flow = M("Leaves") -> find($data['flow_id']);  
        if($data['step']!=100){
            $push_data['type'] = '审批';
            $push_data['action'] = '需要审批';
            $push_data['title'] = '请假';
            $push_data['content'] = '提交人：'.get_dept_name()."-".get_user_name();
            $push_data['url'] = U('selfservice/read',"id={$data['flow_id']}&return_url=selfservice/index");
            send_push($push_data,$user_id);
        }
        return $data;
    }

    function is_last_confirm($flow_id) {
        $confirm = M("Leaves") -> where(array('id' => $flow_id)) -> getField("confirm");
        if (empty($confirm)) {
            return true;
        }
        $confirm_list = array_filter(explode("|", $confirm));
        $last_confirm_emp_no = end($confirm_list);
        if (strpos($last_confirm_emp_no, get_emp_no()) !== false) {
            return true;
        }
        return false;
    }

    function duty_emp_no($flow_id, $step) {
        if (substr($step, 0, 1) == 2) {
            $confirm = M("Leaves") -> where(array('id' => $flow_id)) -> getField("confirm");
            $arr_confirm = array_filter(explode("|", $confirm));
    
            return $arr_confirm[fmod($step, 10) - 1];
        }
    
        if (substr($step, 0, 1) == 3) {
            $consult = M("Leaves") -> where(array('id' => $flow_id)) -> getField("consult");
            $arr_consult = array_filter(explode("|", $consult));
            return $arr_consult[fmod($step, 10) - 1];
        }
    }
 


}
?>